﻿using System;
using System.Diagnostics;

namespace XNET.Core
{
    /// <summary>
    /// CMD命令帮助
    /// </summary>
    public class XCmd
    {
        /// <summary>
        /// 执行CMD命令
        /// </summary>
        /// <param name="cmd">cmd命令</param>
        public static string ExecuteCmd(string cmd)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.StartInfo.CreateNoWindow = true;//不显示程序窗口
            p.Start();//启动程序

            //向cmd窗口发送输入信息
            p.StandardInput.WriteLine(cmd + "&exit");

            p.StandardInput.AutoFlush = true;

            //获取cmd窗口的输出信息
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();

            return output;
        }
        /// <summary>
        /// cmd命令关机
        /// </summary>
        /// <param name="second">N秒后关机</param>
        public static void Shutdown(int second)
        {
            string command = string.Format("shutdown -s -t {0}", second);
            ExecuteCmd(command);
        }
        /// <summary>
        /// cmd命令重启
        /// </summary>
        /// <param name="second"></param>
        public static void Restart(int second)
        {
            string command = string.Format("shutdown -r -t {0}", second);
            ExecuteCmd(command);
        }
        /// <summary>
        /// cmd注销用户
        /// </summary>
        /// <param name="second"></param>
        public static void Logoff(int second)
        {
            string command = string.Format("shutdown -l -t {0}", second);
            ExecuteCmd(command);
        }
        /// <summary>
        /// 中止系统关闭
        /// </summary>
        public static void CancelShutdown()
        {
            ExecuteCmd("shutdown -a");
        }
    }
    /// <summary>
    /// cmd命令
    /// </summary>
    public class Commoncmd
    {
        /// <summary>
        /// 启动服务管理器
        /// </summary>
        /// <returns></returns>
        public static string Services = "services.msc";
        /// <summary>
        /// 启动写字板
        /// </summary>
        public static string Write="write";
        /// <summary>
        /// 启动画笔
        /// </summary>
        public static string MsPaint = "mspaint";
        /// <summary>
        /// 启动远程连接桌面
        /// </summary>
        public static string Mstsc = "mstsc";
        /// <summary>
        /// 启动设备管理器
        /// </summary>
        public static string Devmgmt = "devmgmt.msc";
        /// <summary>
        /// 启动磁盘管理实用程序 
        /// </summary>
        public static string Diskmgmt = "diskmgmt.msc";
        /// <summary>
        /// 打开系统组件服务 
        /// </summary>
        public static string Dcomcnfg = "dcomcnfg";
        /// <summary>
        /// 启动记事本
        /// </summary>
        public static string Notepad = "notepad";
        /// <summary>
        /// 启动文件签名程序
        /// </summary>
        public static string Sigverif = "sigverif";
        /// <summary>
        /// 启动事件管理器
        /// </summary>
        public static string Eventvwr = "eventvwr";
        /// <summary>
        /// 启动任务管理器
        /// </summary>
        public static string Taskmgr = "taskmgr";
        /// <summary>
        /// 打开资源管理器 
        /// </summary>
        public static string Explorer = "explorer";
        /// <summary>
        /// 计算机性能监测程序 
        /// </summary>
        public static string Perfmon = "perfmon.msc";
        /// <summary>
        /// 打开注册表
        /// </summary>
        public static string Regedit = "regedit";
        /// <summary>
        /// 证书管理实用程序 
        /// </summary>
        public static string Certmgr = "certmgr.msc";
        /// <summary>
        /// 启动计算器 
        /// </summary>
        public static string Calc = "calc";
        /// <summary>
        /// SQL SERVER 客户端网络实用程序 
        /// </summary>
        public static string Cliconfg = "cliconfg";
        /// <summary>
        /// 计算机管理 
        /// </summary>
        public static string Compmgmt = "compmgmt.msc";
        /// <summary>
        /// 垃圾整理 
        /// </summary>
        public static string Cleanmgr = "cleanmgr";
        /// <summary>
        /// 打开屏幕键盘  
        /// </summary>
        public static string Osk = "osk";
        /// <summary>
        /// 数据源管理器 
        /// </summary>
        public static string Odbcad32 = "odbcad32";
        /// <summary>
        /// 本机用户和组 
        /// </summary>
        public static string Lusrmgr = "lusrmgr.msc";
        /// <summary>
        /// 注销命令 
        /// </summary>
        public static string Logoff = "logoff";
        /// <summary>
        /// 木马捆绑工具，系统自带 
        /// </summary>
        public static string Iexpress = "iexpress";
        /// <summary>
        /// 共享文件夹管理器 
        /// </summary>
        public static string Fsmgmt = "fsmgmt.msc";
        /// <summary>
        /// 辅助工具管理器
        /// </summary>
        public static string Utilman = "utilman";
        /// <summary>
        /// 组策略
        /// </summary>
        public static string Gpedit = "gpedit.msc";
        /// <summary>
        /// 启动字符映射表
        /// </summary>
        public static string Charmap = "charmap";
    }
}
